package cn.jtool.dig.server.comm.tunnel;

import cn.jtool.dig.common.netty.hander.TunnelDecoder;
import cn.jtool.dig.common.netty.hander.TunnelEncoder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TunnelChannelInitializer extends ChannelInitializer<Channel> {

    private final TunnelConnHander tunnelConnHander;

    private final TunnelDataHander tunnelDataHander;


    public TunnelChannelInitializer(TunnelConnHander tunnelConnHander, TunnelDataHander tunnelDataHander) {
        this.tunnelConnHander = tunnelConnHander;
        this.tunnelDataHander = tunnelDataHander;
    }

    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("decoder", new TunnelDecoder("Server", true));
        pipeline.addLast("encoder", new TunnelEncoder("Server", true));
        pipeline.addLast("connectHandler", this.tunnelConnHander);
        pipeline.addLast("dataHandler", this.tunnelDataHander);
    }
}
